
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
using MonoTouch.ObjCRuntime;
using System.IO;

namespace ComBrowser
{

	[Register("NavChapterView")]
	public partial class NavChapterView : UIView
	{

		private RectangleF rectView;
		CGContext context;
		NSUserDefaults plist = NSUserDefaults.StandardUserDefaults;

		float height = 432;
		float width = 288;
		float topOfGrid = 40;

		float numberOfColumns = 0;
		float numberOfRows = 0;
		float cellWidth = 0;
		float cellHeight = 0;

		int zeroBasedSelectedBookID = 0;

		UIColor colorTitleShadow = UIColor.FromRGBA (106, 68, 33, 50);
		UIColor colorTouchDown = UIColor.FromRGBA (0.4f, 0.5f, 0.2f, 0.5f);

		NavChapterViewController flipViewController;
		BookCollection books;
		Book book;

		public NavChapterView (NSCoder coder) : base(coder)
		{
		}

		public NavChapterView (int SelectedBookID, NavChapterViewController CtrlParent)
		{
			zeroBasedSelectedBookID = SelectedBookID;
			books = BookCollection.Instance ();
			book = books.GetBook (this.zeroBasedSelectedBookID);
			
			this.BackgroundColor = UIColor.Clear;
			RectangleF me = new RectangleF (0, 0, width, height);
			this.Frame = me;
			
			numberOfColumns = GetColumnCount ();
			numberOfRows = GetRowCount ();
			
			cellWidth = width / numberOfColumns;
			cellHeight = (height - topOfGrid) / numberOfRows;
			
			this.flipViewController = CtrlParent;
		}

		public override void Draw (RectangleF rect)
		{
			AddBackgroundImage ();
			AddNavButton ();
			AddTitleLabel ();
			rectView = rect;
			
			context = UIGraphics.GetCurrentContext ();
			DrawInContext (context);
		}

		public void DrawInContext (CGContext ctx)
		{
			UIGraphics.PushContext (ctx);
			ctx.SetAllowsAntialiasing (true);
			AddGrid (ctx);
			AddChapterButtons ();
			UIGraphics.PopContext ();
		}

		void AddBackgroundImage ()
		{
			UIImage bck = UIImage.FromFileUncached (Path.Combine(NSBundle.MainBundle.BundlePath,@"Images/ChapterView.png"));
			if (bck != null)
				UIGraphics.GetCurrentContext ().DrawImage (this.Frame, bck.CGImage);
		}

		void AddTitleLabel ()
		{
			UILabel l = new UILabel (new RectangleF (40, 7, 248, 22));
			l.TextColor = UIColor.White;
			l.Text = book.Long + " Chapters Covered";
			l.Font = UIFont.FromName ("Helvetica-Bold", 15);
			l.BackgroundColor = UIColor.Clear;
			l.ShadowColor = colorTitleShadow;
			l.ShadowOffset = new SizeF (2, 2);
			this.AddSubview (l);
		}

		void AddGrid (CGContext ctx)
		{
			ctx.SaveState ();
			
			float width = this.Frame.Width;
			ctx.SetRGBStrokeColor (77 / 255, 20 / 255, 0, 0.5f);
			
			float heightOfGrid = this.Frame.Height;
			
			for (float i = cellWidth; i < width; i = i + cellWidth) {
				ctx.BeginPath ();
				ctx.MoveTo (i, topOfGrid);
				ctx.AddLineToPoint (i, heightOfGrid);
				ctx.SetLineWidth (0.5f);
				ctx.StrokePath ();
			}
			
			for (float i = cellHeight; i < height; i = i + cellHeight) {
				ctx.BeginPath ();
				ctx.MoveTo (0, i);
				ctx.AddLineToPoint (width, i);
				ctx.SetLineWidth (0.5f);
				ctx.StrokePath ();
			}
			
			ctx.RestoreState ();
		}

		void AddNavButton ()
		{
			UIButton button = new UIButton (new RectangleF (1, 1, 50, 50));
			button.BackgroundColor = UIColor.Clear;
			button.TouchUpInside += (s, e) => { this.flipViewController.SwitchBackToBookView (this.zeroBasedSelectedBookID); };
			this.AddSubview (button);
		}

		void AddChapterButtons ()
		{
			ComDomain comDomain = ComDomain.Instance ();
			int rowNumber = 1;
			int columnNumber = 0;
			for (int chapterNum = 1; chapterNum < book.Chapters + 1; chapterNum++) {
				float Y = rowNumber * cellHeight - 2;
				float X = columnNumber * cellWidth - 2;
				
				int zeroBasedChapter = chapterNum - 1;
				
				if (comDomain.BookHasChapter (book.ZeroBasedBookID, zeroBasedChapter)) {
					RectangleF frame = new RectangleF (X + 3, Y + 3, cellWidth - 2, cellHeight - 2);
					UIButton button = CreateButton (frame, zeroBasedChapter);
				   this.AddSubview (button);
				}
				
				columnNumber++;
				
				if (columnNumber == numberOfColumns) {
					columnNumber = 0;
					rowNumber++;
				}
			}
			 
		}

		//---------------------
		// Helpers
		//---------------------

		UIButton CreateButton (RectangleF frame, int zeroBasedChapterID)
		{
			UIButton button = new UIButton (frame);
			button.SetTitle ((zeroBasedChapterID+1).ToString (), UIControlState.Normal);
			button.SetTitleColor (UIColor.White, UIControlState.Normal);
			button.TitleLabel.Font = UIFont.FromName ("Helvetica", 14);
			button.BackgroundColor = UIColor.Clear;
			button.Tag = zeroBasedChapterID;
			button.TouchDown += (s, e) => ((UIButton)s).BackgroundColor = colorTouchDown;
		
			button.TouchUpInside += (s, e) =>
			{
				((UIButton)s).BackgroundColor = UIColor.Clear;
				this.flipViewController.ChapterPicked (this.zeroBasedSelectedBookID, ((UIButton)s).Tag);
			
			};
			button.TouchUpOutside += (s, e) => ((UIButton)s).BackgroundColor = UIColor.Clear;
			return button;
		}

		int GetColumnCount ()
		{
			int chapterCount = BookCollection.Instance ().GetBook (zeroBasedSelectedBookID).Chapters;
			if (chapterCount < 50) {
				return 5;
			}
			
			if (chapterCount < 90)
				return Convert.ToInt16 (chapterCount / 10);
			
			return 9;
			
		}

		int GetRowCount ()
		{
			int chapterCount = BookCollection.Instance ().GetBook (zeroBasedSelectedBookID).Chapters;
			if (chapterCount < 51) {
				return 10;
			}
			
			if(chapterCount< 90)
				return Convert.ToInt16 (chapterCount / 10) + 4;
			
			return 16;
		}
		
	}
	
	
}
